{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from csv import reader\n",
    "from math import sqrt\n",
    "from datetime import datetime, timezone"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第一题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "剩余 87442 条数据\n"
     ]
    }
   ],
   "source": [
    "def convert(data):\n",
    "    for col in (0, 2, 3, 4):\n",
    "        data[col] = float(data[col])\n",
    "    return data\n",
    "\n",
    "with open(\"gaze.csv\", \"r\") as f:\n",
    "    data = list(reader(f))[1:]\n",
    "\n",
    "data = list(filter(lambda entry: entry[2] >= 0.9, map(convert, data)))\n",
    "total = len(data)\n",
    "\n",
    "print(f\"剩余 {total} 条数据\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第二题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "剩余 86185 条数据\n"
     ]
    }
   ],
   "source": [
    "meanX = sum([x[3] for x in data]) / total\n",
    "meanY = sum([y[4] for y in data]) / total\n",
    "\n",
    "sigmaX = sqrt(sum([(x[3] - meanX) ** 2 for x in data]) / total)\n",
    "sigmaY = sqrt(sum([(y[4] - meanY) ** 2 for y in data]) / total)\n",
    "\n",
    "lBoundX = meanX - 3 * sigmaX\n",
    "lBoundY = meanY - 3 * sigmaY\n",
    "uBoundX = meanX + 3 * sigmaX\n",
    "uBoundY = meanY + 3 * sigmaY\n",
    "\n",
    "data = list(filter(lambda entry: lBoundX <= entry[3] <= uBoundX \\\n",
    "    and lBoundY <= entry[4] <= uBoundY, data))\n",
    "    \n",
    "total = len(data)\n",
    "print(f\"剩余 {total} 条数据\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第三题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1970-01-02T00:29:10.825640+00:00\n",
      "1970-01-02T00:29:10.827730+00:00\n",
      "1970-01-02T00:29:10.829660+00:00\n",
      "1970-01-02T00:29:10.866010+00:00\n",
      "1970-01-02T00:29:10.869620+00:00\n"
     ]
    }
   ],
   "source": [
    "def convertFromTimestamp(entry):\n",
    "    return datetime.utcfromtimestamp(entry).replace(tzinfo = timezone.utc).isoformat(timespec = \"microseconds\")\n",
    "\n",
    "for i in data:\n",
    "    i[0] = convertFromTimestamp(i[0])\n",
    "\n",
    "for i in data[:5]: print(i[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第四题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "采样率为 428 HZ\n"
     ]
    }
   ],
   "source": [
    "with open(\"gaze.csv\", \"r\") as f:\n",
    "    data = list(reader(f))[1:]\n",
    "\n",
    "data = list(map(convert, data))\n",
    "total = len(data)\n",
    "\n",
    "print(f\"采样率为 {total / (data[-1][0] - data[0][0]):.0f} HZ\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第五题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[88150.78, '0', 0.828005946, 0.531432713, 0.397509462, '88150.77678-0', '28.3697529', '52.03313456', '705.1211159', '20', '15', '-20', '0.011526769', '0.05100179', '0.99863204', '', '', '', '', '', '']\n",
      "[88150.79, '1', 0.50259488, 0.340108912, 0.142989495, '88150.787485-1', '-139.4048601', '175.0883214', '681.1514528', '-39.93492801', '14.99791945', '-20.07528253', '-0.136989337', '0.22047545', '0.965724856', '', '', '', '', '', '']\n",
      "[88150.8, '1', 0.708476074, 0.494115709, 0.407109411, '88150.79569-0 88150.801123-1', '2.179628707', '-19.35452993', '-289.3865972', '20', '15', '-20', '0.06514516', '0.027374146', '0.997500258', '-39.93492801', '14.99791945', '-20.07528253', '-0.152522642', '0.222659333', '0.962891305']\n",
      "[88150.81, '1', 0.856340761, 0.494777364, 0.404968539, '88150.809749-0 88150.80479-1', '1.925754581', '-19.71065806', '-288.0721152', '20', '15', '-20', '0.066337739', '0.023839908', '0.997512387', '-39.93492801', '14.99791945', '-20.07528253', '-0.152228442', '0.229814202', '0.961255395']\n",
      "[88150.82, '2', 0.836166061, 0.493772244, 0.400270234, '88150.81599799999-0 88150.820104-1', '2.325815485', '-20.95033508', '-291.7653248', '20', '15', '-20', '0.063962106', '0.024893337', '0.997641805', '-39.93492801', '14.99791945', '-20.07528253', '-0.151527396', '0.234143013', '0.960321039']\n",
      "[88150.83, '2', 0.915197342, 0.493561105, 0.399305458, '88150.827729-0 88150.827729-1', '2.429836652', '-21.37441266', '-294.8192', '20', '15', '-20', '0.062883466', '0.025099742', '0.997705203', '-39.93492801', '14.99791945', '-20.07528253', '-0.150242621', '0.234115497', '0.960529588']\n",
      "[88150.84, '2', 0.898122662, 0.493448859, 0.398483701, '88150.83570099999-0 88150.84344899999-1', '2.469472787', '-21.52512256', '-294.4946166', '20', '15', '-20', '0.062819474', '0.02715739', '0.997655346', '-39.93492801', '14.99791945', '-20.07528253', '-0.150562124', '0.233448037', '0.960642004']\n",
      "[88150.85, '3', 0.814634635, 0.492453122, 0.398923322, '88150.85153599999-0 88150.847474-1', '2.852102899', '-21.48677913', '-295.248611', '20', '15', '-20', '0.061283019', '0.025668513', '0.997790318', '-39.93492801', '14.99791945', '-20.07528253', '-0.151476232', '0.233933283', '0.960380222']\n",
      "[88150.86, '3', 0.832982172, 0.493051011, 0.397207833, '88150.85955699999-0 88150.85956499999-1', '2.653589604', '-22.0797758', '-298.333582', '20', '15', '-20', '0.061303675', '0.026883477', '0.997757054', '-39.93492801', '14.99791945', '-20.07528253', '-0.149151859', '0.234053214', '0.960714742']\n",
      "[88150.87, '3', 0.922197307, 0.492634933, 0.398813752, '88150.867514-0 88150.871726-1', '2.813773751', '-21.74485417', '-298.4712535', '20', '15', '-20', '0.060717022', '0.025219894', '0.997836359', '-39.93492801', '14.99791945', '-20.07528253', '-0.149645418', '0.233257839', '0.960831426']\n",
      "[88150.88, '4', 0.945000471, 0.492159468, 0.39885528, '88150.879586-0 88150.87958299999-1', '2.996871523', '-21.74644911', '-298.6157017', '20', '15', '-20', '0.060040362', '0.024936294', '0.99788443', '-39.93492801', '14.99791945', '-20.07528253', '-0.150193199', '0.233408853', '0.960709275']\n",
      "[88150.89, '4', 0.825606805, 0.492182499, 0.397400267, '88150.888291-0 88150.891702-1', '3.013482745', '-22.24690193', '-301.1555089', '20', '15', '-20', '0.05943898', '0.025782407', '0.99789893', '-39.93492801', '14.99791945', '-20.07528253', '-0.148915824', '0.233733232', '0.960829253']\n",
      "[88150.9, '4', 0.821102522, 0.491259971, 0.398451849, '88150.89951199999-0 88150.899511-1', '3.355394428', '-21.92932424', '-299.930569', '20', '15', '-20', '0.058503551', '0.025052783', '0.997972792', '-39.93492801', '14.99791945', '-20.07528253', '-0.150724008', '0.233360652', '0.96063785']\n",
      "[88150.91, '4', 0.860093312, 0.491499686, 0.398430303, '88150.90741599999-0 88150.91137999999-1', '3.282349615', '-22.06157621', '-301.67539', '20', '15', '-20', '0.058394789', '0.024000476', '0.998005023', '-39.93492801', '14.99791945', '-20.07528253', '-0.149559391', '0.233747564', '0.960725801']\n",
      "[88150.92, '5', 0.856588177, 0.491390903, 0.396667245, '88150.91942399999-0 88150.919423-1', '3.336274886', '-22.52499837', '-302.75705', '20', '15', '-20', '0.057982053', '0.026013439', '0.997978648', '-39.93492801', '14.99791945', '-20.07528253', '-0.149174388', '0.233956834', '0.96073472']\n",
      "[88150.93, '5', 0.853040895, 0.491452092, 0.397484682, '88150.927522-0 88150.92752699999-1', '3.323771199', '-22.42242492', '-303.7814987', '20', '15', '-20', '0.057815107', '0.023935124', '0.998040341', '-39.93492801', '14.99791945', '-20.07528253', '-0.148601659', '0.234394242', '0.960716861']\n",
      "[88150.94, '5', 0.998744396, 0.491161623, 0.396321228, '88150.93622799999-0 88150.94089099999-1', '3.44467066', '-22.72937798', '-304.4845144', '20', '15', '-20', '0.057255736', '0.025567573', '0.998032104', '-39.93492801', '14.99791945', '-20.07528253', '-0.148647506', '0.23424002', '0.960747382']\n",
      "[88150.95, '6', 0.803616379, 0.491623894, 0.396117888, '88150.947523-0 88150.947523-1', '3.262791263', '-22.76199906', '-304.3246522', '20', '15', '-20', '0.057914416', '0.025849307', '0.99798684', '-39.93492801', '14.99791945', '-20.07528253', '-0.148117476', '0.234331061', '0.96080704']\n",
      "[88150.96, '6', 0.839118919, 0.491027779, 0.397470578, '88150.963521-0 88150.95548399999-1', '3.496927261', '-22.47799933', '-304.4925348', '20', '15', '-20', '0.057074997', '0.023794625', '0.998086299', '-39.93492801', '14.99791945', '-20.07528253', '-0.148820867', '0.234277045', '0.960711515']\n",
      "[88150.97, '6', 0.892754139, 0.490925743, 0.396928939, '88150.96744-0 88150.97157499999-1', '3.554080369', '-22.70781996', '-305.9892716', '20', '15', '-20', '0.056584138', '0.024528738', '0.998096476', '-39.93492801', '14.99791945', '-20.07528253', '-0.14825732', '0.233794742', '0.960916118']\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "rate = 0.01\n",
    "resampled = []\n",
    "\n",
    "def getTime(i):\n",
    "    return data[i][0]\n",
    "\n",
    "while i < total- 1:\n",
    "    time = getTime(i)\n",
    "    nextTime = time + rate\n",
    "    prevTime = False\n",
    "    data[i][0] = round(time, 2)\n",
    "    resampled.append(data[i])\n",
    "    while i < total - 1:\n",
    "        i += 1\n",
    "        if getTime(i) - time > rate:\n",
    "            if not prevTime: break\n",
    "            if nextTime - getTime(i - 1) < getTime(i) - nextTime:\n",
    "                i -= 1\n",
    "                break\n",
    "        prevTime = True\n",
    "\n",
    "for i in resampled[:20]: print(i)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5c526b1e5de57029c5e5da12a7f8e7eff8371891e90fe1bf7d3f01d6d2e327c7"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
